<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" labelWidth="100" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <span>维修项目主分类列表</span>
    <FilterTable
      ref="table"
      :columns="columns2"
      :isSelectColumn="false"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'data.rows'"
      :height="200"
      :onColumnsChange="columns => (this.columns = columns)"
      :onEnterEvent="selectChangeHandle"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
        <!-- <el-button size="small">导出</el-button> -->
      </template>
    </FilterTable>
    <subGroupTable :dataSource="subGroupList" :mainInfo="mainInfo" />
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose width="500px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <mainGroup :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import mainGroup from './mainGroup';
import subGroupTable from './subGroupTable';
import { getMainGroupList, getSubGroupList } from '@/api/repair/repair/labourGroup';

export default {
  name: 'Ordermanage',
  components: {
    mainGroup,
    subGroupTable
  },
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      fetchApi: getMainGroupList,
      dialogTitle: '',
      fetchParams: { xhrAbort: true },
      columns2: this.createTableColumns(),
      handleType: '',
      formData: {},
      subGroupList: [],
      mainInfo: {}
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 200,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" disabled={this.isEdit(props.row)} onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
                <el-divider direction="vertical" />
              </div>
            );
          }
        },

        {
          title: '主分类代码',
          dataIndex: 'mainGroupCode',
          //  width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '主分类名称',
          dataIndex: 'mainGroupName',
          // width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '数据来源',
          dataIndex: 'donwTag',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1045),
          render: props => {
            return <span>{this.changgeDictYN(props.row.downTag)}</span>;
          }
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '主分类代码',
          fieldName: 'mainGroupCode',
          placeholder: '主分类代码...'
        },
        {
          type: 'INPUT',
          label: '主分类名称',
          fieldName: 'mainGroupName',
          placeholder: '主分类名称...'
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'downTag',
          placeholder: '请选择',
          itemList: this.createDictList(1045)
        }
      ];
    },
    isEdit(row) {
      if (row.downTag === 12781002) {
        return false;
      } else {
        return true;
      }
    },
    selectChangeHandle(row) {
      if (!row) {
        this.subGroupList = [];
        this.mainInfo = {};
        return;
      }
      console.log(row);
      this.getSubGroupList(row);
    },
    async getSubGroupList(row) {
      console.log(22222222222);
      const res = await getSubGroupList(row.mainGroupCode, row);
      console.log('111', res);
      this.subGroupList = res.data.rows;
      this.mainInfo = row;
      // this.parmes.list1 = res;
    },

    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
      this.subGroupList = [];
      this.mainInfo = {};
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      // console.log(this.formData);
      this.showHandler();
    },

    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}维修项目主类`;
      this.visible = true;
    },
    changgeDictYN(val) {
      if (val == '10041001') return '是';
      else return '否';
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
        this.subGroupList = [];
        this.mainInfo = {};
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
